package com.lz.service.impl;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.lz.service.FileUserService;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;

/**
 * <pre>
 * @描述：
 * @作者：lizhao
 * @创建时间： 2024/8/26 上午9:28
 * </pre>
 **/
@Service
@Data
@ConfigurationProperties(prefix = "aliyun.oss")
public class FileServiceImpl implements FileUserService {

    private String endpoint;
    private String accessKeyId;
    private String accessKeySecret;
    private String bucketName;


    public void uploadObject(String objectName,
                             InputStream file) {
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        try {
            ossClient.putObject(bucketName, objectName, file);
        } catch (Exception oe) {
            System.out.println("出现异常");
        } finally {
            ossClient.shutdown();
        }
    }


    @Override
    public String uploadFile(MultipartFile file) throws Exception {

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        try {
            ossClient.putObject(bucketName, file.getOriginalFilename(), file.getInputStream());
        } catch (Exception oe) {
            System.out.println("出现异常");
        } finally {
            ossClient.shutdown();
        }
        return "\n" +
                "https://lz111.oss-cn-beijing.aliyuncs.com/"  + file.getOriginalFilename();
    }
}
